﻿// Copyright (c) 2007-2021 西安交通信息投资营运有限公司 版权所有
// 作者:吕艳阳

using System;
using System.Collections.Generic;
using System.Runtime.Caching;
using System.Windows.Forms;
using XCI.Core;
using XCI.Helper;
using XCI.Sys.Helper;
using XCI.Sys.Model;
using XCI.Sys.Service;

namespace XCI.Sys
{
    /// <summary>
    /// 权限子系统应用对象
    /// </summary>
    public static class SysApp
    {
        /// <summary>
        /// 权限子系统处理器实例
        /// </summary>
        public static SysAppHandler Handler => (SysAppHandler)App.Handler;

        /// <summary>
        /// 当前登陆用户
        /// </summary>
        public static SysUser CurrentUser { get; set; }

        /// <summary>
        /// 主窗体
        /// </summary>
        public static Form MainForm { get; internal set; }

        /// <summary>
        /// 主窗体管理器
        /// </summary>
        public static MainFormManager MainFormManager { get; internal set; }

        /// <summary>
        /// 获取当前登陆用户拥有的模块集合
        /// </summary>
        public static List<SysModule> GetCurrentUserOwnModules()
        {
            var cacheList = MemoryCache.Default.Get(R.CurrentUserOwnModulesCacheKey);
            if (cacheList == null)
            {
                RefreshCurrentUserOwnModules();
                cacheList = MemoryCache.Default.Get(R.CurrentUserOwnModulesCacheKey);
            }
            if (cacheList != null)
            {
                return (List<SysModule>)cacheList;
            }
            return new List<SysModule>();
        }

        /// <summary>
        /// 刷新当前登陆用户拥有的模块集合
        /// </summary>
        public static void RefreshCurrentUserOwnModules()
        {
            if (CurrentUser == null) return;
            var userPermission = AsyncHelper.RunSync(() => SysAccountService.Instance.SelectUserPermissionAsync(CurrentUser.Id));
            MemoryCache.Default.Set(R.CurrentUserOwnModulesCacheKey, userPermission.Modules, DateTimeOffset.Now.AddHours(12));
        }

        /// <summary>
        /// 是否有指定模块编码的权限
        /// </summary>
        /// <param name="codes">模块编码集合</param>
        /// <returns>存在返回true,否则返回false</returns>
        public static bool Permit(params string[] codes)
        {
            if (WinFormHelper.IsDesignMode()) return false;
            return Handler.Permit(codes);
        }

        /// <summary>
        /// 主窗体添加Tab文档
        /// </summary>
        /// <param name="moduleInfo">模块信息</param>
        public static void AddMainFormDocument(SysModule moduleInfo)
        {
            MainFormManager?.AddDocument(moduleInfo);
        }

        /// <summary>
        /// 主窗体添加Tab文档
        /// </summary>
        /// <param name="formProviderInfo">窗体信息</param>
        public static void AddMainFormDocument(FormProviderInfo formProviderInfo)
        {
            MainFormManager?.AddDocument(formProviderInfo);
        }

        /// <summary>
        /// 主窗体添加Tab文档
        /// </summary>
        /// <param name="formInfo">窗体信息</param>
        public static void AddMainFormDocument(FormInfo formInfo)
        {
            MainFormManager?.AddDocument(formInfo);
        }
    }
}